
package uestc.bitman.edxapp.courses;

import uestc.bitman.edxapp.EdxAppConfigurations;
import uestc.bitman.edxapp.R;
import uestc.bitman.edxapp.cache.ImageCacheManager;
import uestc.bitman.edxapp.courselist.Course;
import uestc.bitman.edxapp.mainui.MainActivity;
import uestc.bitman.edxapp.misc.ChangeEnrollmentAsyncTask;
import uestc.bitman.edxapp.misc.ChangeEnrollmentParameters;

import android.app.Fragment;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AboutCourseFragment extends Fragment {

    private Context mContext;

    private Course mCourse;

    private TextView mUniversity, mShortDescription, mCourseName;

    private Button mRegisterButton;

    private ImageView mCourseImage;

    private Drawable mDefaultIamgeDrawable;

    private Drawable mErrorImageDrawable;

    public AboutCourseFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        mDefaultIamgeDrawable = mContext.getResources().getDrawable(R.drawable.ic_stub);
        mErrorImageDrawable = mContext.getResources().getDrawable(R.drawable.ic_empty);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        // when switch to this page,update the title and enable the press again
        // to exit(because this page is at home)
        ((MainActivity) mContext).enablePressAgainToExit(false);
        getActivity().setTitle(mCourse.getDisplayName());

        View view = initLayout(inflater, container, savedInstanceState);

        return view;
    }

    private View initLayout(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.about_course_fragment, container, false);

        mCourseName = (TextView) view.findViewById(R.id.course_name);
        mCourseImage = (ImageView) view.findViewById(R.id.course_image);
        mUniversity = (TextView) view.findViewById(R.id.course_university);
        mShortDescription = (TextView) view.findViewById(R.id.course_short_description);

        mRegisterButton = (Button) view.findViewById(R.id.register_course);

        mCourseName.setText(mCourse.getDisplayName());
        mUniversity.setText(mCourse.getUniversity());
        mShortDescription.setText(mCourse.getShortDescription().equals("") ? mContext
                .getString(R.string.label_lazy_teacher) : mCourse.getShortDescription());

        ImageCacheManager.loadImage(EdxAppConfigurations.createRequestUrl(mCourse.getImageUrl()),
                ImageCacheManager.getImageListener(mCourseImage, mDefaultIamgeDrawable,
                        mErrorImageDrawable));

        mRegisterButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                ChangeEnrollmentParameters parameter = new ChangeEnrollmentParameters() {
                    @Override
                    public void onPostExecute(boolean result) {
                        Toast.makeText(mContext, mContext.getString(R.string.label_ok_enroll),
                                Toast.LENGTH_SHORT).show();
                    }
                };
                parameter.setAction(EdxAppConfigurations.CHANGE_ENROLLMENT_ENROLL_ACTION);
                parameter.setCourseId(mCourse.getCourseId());
                ChangeEnrollmentAsyncTask enrollAsyncTask = new ChangeEnrollmentAsyncTask();
                enrollAsyncTask.execute(parameter);
                ((MainActivity) mContext).onBackPressed();
            }
        });
        return view;
    }

    public void setCourse(Course course) {
        mCourse = course;
    }
}
